﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using Hutech.Authentication.Library.Data;
using System.Data;

namespace Hutech.Authentication.Library.Data
{
    public static class DBActions
    {
        /// <summary>
        /// Gets the connection string for read.
        /// </summary>
        /// <returns></returns>
        private static string GetReadConnectionString()
        {
            return ConfigurationManager.AppSettings["MSSQLConnectionString_CustommerService27032013"];
        }

        /// <summary>
        /// Gets the connection string for write.
        /// </summary>
        /// <returns></returns>
        private static string GetWriteConnectionString()
        {
            if (ConfigurationManager.AppSettings["MSSQLWriteConnectionString_CustommerService27032013"] != null)
            {
                return ConfigurationManager.AppSettings["MSSQLWriteConnectionString_CustommerService27032013"];
            }

            return ConfigurationManager.AppSettings["MSSQLConnectionString_CustommerService27032013"];
        }


        /// <summary>
        /// Inserts a row in the root_Actions table. Returns rows affected count.
        /// </summary>
        /// <param name="actionKindGuid"> actionKindGuid </param>
        /// <param name="actionKindName"> actionKindName </param>
        /// <param name="actionGuid"> actionGuid </param>
        /// <param name="actionName"> actionName </param>
        /// <param name="description"> description </param>
        /// <param name="actionCommand"> actionCommand </param>
        /// <returns>int</returns>
        public static int Create(
            Guid actionKindGuid,
            string actionKindName,
            Guid actionGuid,
            string actionName,
            string description,
            string actionCommand)
        {
            SqlParameterHelper sph = new SqlParameterHelper(GetWriteConnectionString(), "root_Actions_Insert", 6);
            sph.DefineSqlParameter("@ActionKindGuid", SqlDbType.UniqueIdentifier, ParameterDirection.Input, actionKindGuid);
            sph.DefineSqlParameter("@ActionKindName", SqlDbType.NVarChar, 256, ParameterDirection.Input, actionKindName);
            sph.DefineSqlParameter("@ActionGuid", SqlDbType.UniqueIdentifier, ParameterDirection.Input, actionGuid);
            sph.DefineSqlParameter("@ActionName", SqlDbType.NVarChar, 256, ParameterDirection.Input, actionName);
            sph.DefineSqlParameter("@Description", SqlDbType.NVarChar, 256, ParameterDirection.Input, description);
            sph.DefineSqlParameter("@ActionCommand", SqlDbType.NVarChar, 256, ParameterDirection.Input, actionCommand);
            int rowsAffected = sph.ExecuteNonQuery();
            return rowsAffected;

        }


        /// <summary>
        /// Updates a row in the root_Actions table. Returns true if row updated.
        /// </summary>
        /// <param name="actionKindGuid"> actionKindGuid </param>
        /// <param name="actionKindName"> actionKindName </param>
        /// <param name="actionGuid"> actionGuid </param>
        /// <param name="actionName"> actionName </param>
        /// <param name="description"> description </param>
        /// <param name="actionCommand"> actionCommand </param>
        /// <returns>bool</returns>
        public static bool Update(
            Guid actionKindGuid,
            Guid actionGuid,
            string actionKindName,
            string actionName,
            string description,
            string actionCommand)
        {
            SqlParameterHelper sph = new SqlParameterHelper(GetWriteConnectionString(), "root_Actions_Update", 6);
            sph.DefineSqlParameter("@ActionKindGuid", SqlDbType.UniqueIdentifier, ParameterDirection.Input, actionKindGuid);
            sph.DefineSqlParameter("@ActionGuid", SqlDbType.UniqueIdentifier, ParameterDirection.Input, actionGuid);
            sph.DefineSqlParameter("@ActionKindName", SqlDbType.NVarChar, 256, ParameterDirection.Input, actionKindName);
            sph.DefineSqlParameter("@ActionName", SqlDbType.NVarChar, 256, ParameterDirection.Input, actionName);
            sph.DefineSqlParameter("@Description", SqlDbType.NVarChar, 256, ParameterDirection.Input, description);
            sph.DefineSqlParameter("@ActionCommand", SqlDbType.NVarChar, 256, ParameterDirection.Input, actionCommand);
            int rowsAffected = sph.ExecuteNonQuery();
            return (rowsAffected > 0);

        }

        /// <summary>
        /// Deletes a row from the root_Actions table. Returns true if row deleted.
        /// </summary>
        /// <param name="actionGuid"> actionGuid </param>
        /// <returns>bool</returns>
        public static bool Delete(
            Guid actionGuid)
        {
            SqlParameterHelper sph = new SqlParameterHelper(GetWriteConnectionString(), "root_Actions_Delete", 1);
            sph.DefineSqlParameter("@ActionGuid", SqlDbType.UniqueIdentifier, ParameterDirection.Input, actionGuid);
            int rowsAffected = sph.ExecuteNonQuery();
            return (rowsAffected > 0);

        }

        /// <summary>
        /// Gets an IDataReader with one row from the root_Actions table.
        /// </summary>
        /// <param name="actionGuid"> actionGuid </param>
        public static IDataReader GetOne(
            Guid actionGuid)
        {
            SqlParameterHelper sph = new SqlParameterHelper(GetReadConnectionString(), "root_Actions_SelectOne", 1);
            sph.DefineSqlParameter("@ActionGuid", SqlDbType.UniqueIdentifier, ParameterDirection.Input, actionGuid);
            return sph.ExecuteReader();

        }

        /// <summary>
        /// Gets a count of rows in the root_Actions table.
        /// </summary>
        public static int GetCount()
        {

            return Convert.ToInt32(SqlHelper.ExecuteScalar(
                GetReadConnectionString(),
                CommandType.StoredProcedure,
                "root_Actions_GetCount",
                null));

        }

        /// <summary>
        /// Gets an IDataReader with all rows in the root_Actions table.
        /// </summary>
        public static IDataReader GetAll()
        {

            return SqlHelper.ExecuteReader(
                GetReadConnectionString(),
                CommandType.StoredProcedure,
                "root_Actions_SelectAll",
                null);

        }

        /// <summary>
        /// Gets a page of data from the root_Actions table.
        /// </summary>
        /// <param name="pageNumber">The page number.</param>
        /// <param name="pageSize">Size of the page.</param>
        /// <param name="totalPages">total pages</param>
        public static IDataReader GetPage(
            int pageNumber,
            int pageSize,
            out int totalPages)
        {
            totalPages = 1;
            int totalRows
                = GetCount();

            if (pageSize > 0) totalPages = totalRows / pageSize;

            if (totalRows <= pageSize)
            {
                totalPages = 1;
            }
            else
            {
                int remainder;
                Math.DivRem(totalRows, pageSize, out remainder);
                if (remainder > 0)
                {
                    totalPages += 1;
                }
            }

            SqlParameterHelper sph = new SqlParameterHelper(GetReadConnectionString(), "root_Actions_SelectPage", 2);
            sph.DefineSqlParameter("@PageNumber", SqlDbType.Int, ParameterDirection.Input, pageNumber);
            sph.DefineSqlParameter("@PageSize", SqlDbType.Int, ParameterDirection.Input, pageSize);
            return sph.ExecuteReader();

        }


        internal static IDataReader GetAllKindAction()
        {
            return SqlHelper.ExecuteReader(
               GetReadConnectionString(),
               CommandType.StoredProcedure,
               "root_Actions_SelectAllKindAction",
               null);
        }
    }

}

